﻿using System;
using System.Linq;

namespace CueMounter.DokanStructure
{
	public abstract class DokanDirectory : IDisposable
	{
		public string Name { get; set; }

		public virtual void Dispose()
		{
		}

		public virtual DokanDirectory[] Directories
		{
			get
			{
				return null;
			}
		}

		public virtual DokanDirectory GetDirectory(string name)
		{
			DokanDirectory[] directories = Directories;
			if(directories == null)
			{
				return null;
			}
			for(int i = 0; i < directories.Length; ++i)
			{
				if(directories[i].Name == name)
				{
					return directories[i];
				}
			}
			return null;
		}

		public virtual DokanDirectory GetDirectory(string[] path)
		{
			DokanDirectory directory = this;
			foreach(var part in path)
			{
				directory = directory.GetDirectory(part);
				if(directory == null)
				{
					return null;
				}
			}
			return directory;
		}

		public virtual DokanFile[] Files
		{
			get
			{
				return null;
			}
		}

		public virtual DokanFile GetFile(string name)
		{
			DokanFile[] files = Files;
			if(files == null)
			{
				return null;
			}
			for(int i = 0; i < files.Length; ++i)
			{
				if(files[i].Name == name)
				{
					return files[i];
				}
			}
			return null;
		}

		public virtual DokanFile GetFile(string[] path)
		{
			DokanDirectory directory = this;
			for(int i = 0; i < path.Length - 1; ++i)
			{
				directory = directory.GetDirectory(path[i]);
				if(directory == null)
				{
					return null;
				}
			}
			return directory.GetFile(path[path.Length - 1]);
		}

		public virtual void CreateDirectory(string name)
		{
			throw new NotSupportedException();
		}

		public virtual void DeleteDirectory(string name)
		{
			throw new NotSupportedException();
		}

		public virtual void CreateFile(string name)
		{
			throw new NotSupportedException();
		}

		public virtual void DeleteFile(string name)
		{
			throw new NotSupportedException();
		}

		public virtual DokanFileDates Dates
		{
			get
			{
				DateTime now = DateTime.Now;
				DateTime before = DateTime.Now.Subtract(TimeSpan.FromHours(1));
				return new DokanFileDates(before, now, now);
			}
			set
			{
			}
		}

		public virtual void MoveFile(string[] oldPath, string[] newPath, bool replace)
		{
			// TODO: Simulate move (create, copy, delete)
			throw new NotSupportedException();
		}

		public virtual void MoveDirectory(string[] oldPath, string[] newPath, bool replace)
		{
			// TODO: Simualte moving directory, recursive
			throw new NotSupportedException();
		}
	}
}
